package com.jdz.gxq.info.admin.estate.service;

import java.util.List;
import java.util.Map;

import jp.terasoluna.fw.dao.QueryDAO;
import jp.terasoluna.fw.dao.UpdateDAO;

import com.jdz.gxq.info.admin.estate.bo.Estate;

public class EstateServiceImpl implements EstateService {
	
	private QueryDAO queryDAO = null;
	
	private UpdateDAO updateDAO = null;
	
	/**
	 * Estate检索分页用，查询记录条数
	 * @return
	 */
	public int getEstatesCount(String enterpriseName) {
		
		int totalCount = queryDAO.executeForObject("estate.getEstatesCount",
				enterpriseName, Integer.class);
		
		return totalCount;
	}
	
	/**
	 * Estate检索
	 * @return
	 */
	public List<Estate> getEstates(Map<String, Object> params) {
		
		return queryDAO.executeForObjectList("estate.getEstates", params);
	}
	
	/**
	 * 追加Estate
	 * @param estate
	 */
	public void insertEstate(Estate estate) {
		
		updateDAO.execute("estate.insertEstate", estate);
	}
	
	/**
	 * 更新Estate
	 * @param estate
	 */
	public void updateEstate(Estate estate) {
		
		updateDAO.execute("estate.updateEstate", estate);
	}
	
	/**
	 * 通过id得到estate
	 * @param id
	 * @return
	 */
	public Estate getEstateById(int enterpriseId) {

		Estate estate = queryDAO.executeForObject("estate.getEstateById",
				enterpriseId, Estate.class);
		
		return estate;
	}
	
	/**
	 * 删除Estate
	 * @param estate
	 */
	public void deleteEstate(int enterpriseId) {
		
		updateDAO.execute("estate.deleteEstateById", enterpriseId);
	}
	
	/**
	 * 验证estateName是否重复
	 * flag: 1 追加时验证
	 * flag: 2 更新时验证
	 */
	public boolean checkEstateName(Estate paramEstate, int flag) {
		
		Estate estate = queryDAO.executeForObject("estate.getEstateByName",
				paramEstate.getEnterpriseName(), Estate.class);

		if (estate == null) {
			return true;
		}
		
		// 更新验证，查出来的数据是不是本身
		if (flag == 2) {
			
			if (estate.getEnterpriseId() == paramEstate.getEnterpriseId()) {
				return true;
			}
		}
		
		return false;
	}

	public void setQueryDAO(QueryDAO queryDAO) {
		this.queryDAO = queryDAO;
	}

	public void setUpdateDAO(UpdateDAO updateDAO) {
		this.updateDAO = updateDAO;
	}
	
	
}
